package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import flc.ast.App;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.PasswordAdapter;
import flc.ast.bean.MyPassKindBean;
import flc.ast.bean.MyPassRecordBean;
import flc.ast.databinding.ActivityPasswordBinding;
import flc.ast.dialog.DeleteDialog;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

public class PasswordActivity extends BaseAc<ActivityPasswordBinding> {

    public static MyPassKindBean kindBean;
    private boolean isEdit;
    private PasswordAdapter passwordAdapter;
    private List<MyPassRecordBean> listShow = new ArrayList<>();


    @Override
    protected int onCreate() {
        return R.layout.activity_password;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivPasswordBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvPasswordEdit.setOnClickListener(this);
        mDataBinding.tvPasswordComplete.setOnClickListener(this);
        mDataBinding.tvGoAdd.setOnClickListener(this);
        mDataBinding.tvAddPassword.setOnClickListener(this);

        mDataBinding.rvPasswordList.setLayoutManager(new LinearLayoutManager(mContext));
        passwordAdapter = new PasswordAdapter();
        mDataBinding.rvPasswordList.setAdapter(passwordAdapter);
        passwordAdapter.setEdit(isEdit);
        passwordAdapter.addChildClickViewIds(R.id.ivPasswordItemDelete);
        passwordAdapter.setOnItemClickListener(this);
        passwordAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvPasswordEdit://编辑
                if (listShow == null || listShow.size() == 0) return;
                isEdit = true;
                passwordAdapter.setEdit(isEdit);
                passwordAdapter.notifyDataSetChanged();
                mDataBinding.tvPasswordEdit.setVisibility(View.GONE);
                mDataBinding.tvPasswordComplete.setVisibility(View.VISIBLE);
                break;
            case R.id.tvPasswordComplete://完成
                cancelEdit();
                break;
            case R.id.tvGoAdd://添加密码
            case R.id.tvAddPassword:
                AddPasswordActivity.kindBean = kindBean;
                startActivity(AddPasswordActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (!isEdit) {
            PasswordDetailActivity.position = position;
            startActivity(PasswordDetailActivity.class);
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivPasswordItemDelete://删除
                showDeleteDialog(position);
                break;
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvPasswordTitle.setText(kindBean.getKind() + getString(R.string.password));
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    //获取记录数据
    private void getData() {
        listShow.clear();
        List<MyPassKindBean> listSp = AppSpUtils.getPasswordList();
        for (MyPassKindBean myPassKindBean : listSp) {
            if (myPassKindBean.getKind().equals(kindBean.getKind())) {
                if (myPassKindBean.getList() != null && myPassKindBean.getList().size() > 0) {
                    listShow.addAll(myPassKindBean.getList());
                }
            }
        }
        if (listShow != null && listShow.size() > 0) {
            passwordAdapter.setList(listShow);
            mDataBinding.rvPasswordList.setVisibility(View.VISIBLE);
            mDataBinding.llPasswordNoData.setVisibility(View.GONE);
            mDataBinding.tvAddPassword.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvPasswordList.setVisibility(View.GONE);
            mDataBinding.llPasswordNoData.setVisibility(View.VISIBLE);
            mDataBinding.tvAddPassword.setVisibility(View.GONE);
        }
    }

    //删除记录
    private void deleteRecord(int position) {
        List<MyPassKindBean> listSp = AppSpUtils.getPasswordList();
        for (MyPassKindBean myPassKindBean : listSp) {
            if (myPassKindBean.getKind().equals(kindBean.getKind())) {
                myPassKindBean.getList().remove(position);
            }
        }
        //重新保存
        AppSpUtils.savePasswordList(listSp);
        ToastUtils.showShort(getString(R.string.delete_suc));
        //总条数+1
        int countPass = SPUtil.getInt(mContext, Content.countPass, 0);
        countPass--;
        SPUtil.putInt(mContext, Content.countPass, countPass);
        //重新获取记录
        cancelEdit();
    }

    private void cancelEdit() {
        isEdit = false;
        passwordAdapter.setEdit(isEdit);
        getData();
        mDataBinding.tvPasswordEdit.setVisibility(View.VISIBLE);
        mDataBinding.tvPasswordComplete.setVisibility(View.GONE);
    }

    private void showDeleteDialog(int position) {
        DeleteDialog dialog = new DeleteDialog(mContext);
        dialog.setListener(new DeleteDialog.Ilistener() {

            @Override
            public void clickDelete() {
                deleteRecord(position);
            }

        });
        dialog.show();
    }

}